using System;
using System.Collections.Generic;
using System.Windows.Forms;


namespace Atom.Utility.Enums
{
   
    public static class EnumHelper
    {
        public static void BindEnumToComboBox<T>(ComboBox cb) where T: struct
        {
            BindEnumToComboBox<T>(cb, null, null);
        }

        public static void BindEnumToComboBox<T>(ComboBox cb, string firstItemText, string firstItemValue) where T: struct
        {
            List<KeyValuePair<string, string>> list = LoadEnum<T>().ConvertAll<KeyValuePair<string, string>>(delegate (KeyValuePair<T, EnumMapperAttribute> _input) {
                return new KeyValuePair<string, string>(_input.Key.ToString(), (_input.Value != null) ? _input.Value.Description : _input.Key.ToString());
            });
            if ((firstItemText != null) && (firstItemValue != null))
            {
                KeyValuePair<string, string> item = new KeyValuePair<string, string>(firstItemValue, firstItemText);
                list.Insert(0, item);
            }
            cb.DataSource = list;
            cb.DisplayMember = "Value";
            cb.ValueMember = "Key";
        }

        public static bool CheckIsTypeEnum(System.Type enumType, bool isThrowException)
        {
            if (enumType == null)
            {
                if (isThrowException)
                {
                    throw new NullReferenceException(string.Format("类型参数{0}不能为空", "enumType"));
                }
                return false;
            }
            if (!enumType.IsEnum)
            {
                if (isThrowException)
                {
                    throw new Exception(string.Format("类型{0}必须要为枚举", enumType.FullName));
                }
                return false;
            }
            return true;
        }

        public static string LoadDescription<T>(object obj) where T: struct
        {
            if (CheckIsTypeEnum(typeof(T), true))
            {
                return LoadDescription<T>((T) Enum.Parse(typeof(T), obj.ToString()));
            }
            return "";
        }

        public static string LoadDescription<T>(T t) where T: struct
        {
            string str = t.ToString();
            foreach (KeyValuePair<T, EnumMapperAttribute> pair in LoadEnum<T>())
            {
                if (pair.Key.Equals(t))
                {
                    return pair.Value.Description;
                }
            }
            return str;
        }

        public static List<KeyValuePair<T, EnumMapperAttribute>> LoadEnum<T>() where T: struct
        {
            Action<T> action = null;
            List<KeyValuePair<T, EnumMapperAttribute>> list = new List<KeyValuePair<T, EnumMapperAttribute>>();
            if (CheckIsTypeEnum(typeof(T), true))
            {
                if (action == null)
                {
                    action = delegate (T _enumData) {
                        list.Add(new KeyValuePair<T, EnumMapperAttribute>(_enumData, EnumMapperAttribute.GetSelf(typeof(T).GetField(_enumData.ToString()))));
                    };
                }
                Array.ForEach<T>(Enum.GetValues(typeof(T)) as T[], action);
            }
            return list;
        }

        public static T LoadEnumComboBoxCurrentSelect<T>(ComboBox cb) where T: struct
        {
            T local = default(T);
            KeyValuePair<string, string> selectedItem = (KeyValuePair<string, string>) cb.SelectedItem;
            string key = selectedItem.Key;
            if (Enum.IsDefined(typeof(T), key))
            {
                local = (T) Enum.Parse(typeof(T), key);
            }
            return local;
        }

        public static EnumMapperAttribute LoadMapper<T>(object obj) where T: struct
        {
            return LoadMapper<T>((T) Enum.Parse(typeof(T), obj.ToString()));
        }

        public static EnumMapperAttribute LoadMapper<T>(T t) where T: struct
        {
            if (CheckIsTypeEnum(typeof(T), true))
            {
                foreach (KeyValuePair<T, EnumMapperAttribute> pair in LoadEnum<T>())
                {
                    if (pair.Key.Equals(t))
                    {
                        return pair.Value;
                    }
                }
            }
            return null;
        }
    }
}
